<?php

namespace app\admin\model\scanwork;

use think\Model;

class MrpPlan extends Model
{
    protected $name = 'scanwork_mrp_plan';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';

    public function requirements()
    {
        return $this->hasMany('MrpRequirement', 'plan_id', 'id');
    }

    public static function generatePlanNo()
    {
        $prefix = 'MRP-';
        $date = date('Ymd');
        $lastPlan = self::where('plan_no', 'like', $prefix . $date . '%')
            ->order('id', 'desc')
            ->value('plan_no');
        
        if ($lastPlan) {
            $lastNum = intval(substr($lastPlan, -4));
            $newNum = str_pad($lastNum + 1, 4, '0', STR_PAD_LEFT);
        } else {
            $newNum = '0001';
        }
        
        return $prefix . $date . $newNum;
    }
}

